package com.maolong.controller;

import com.maolong.pojo.fuction.result.Result;
import com.maolong.util.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@RestController
@Slf4j
@Api("通用接口")
@RequestMapping("/common")
public class CommonController {

    @Autowired
    AliOssUtil aliOssUtil;
    /**
     * 上传图片
     */
    @RequestMapping("/uploadImage")
    public Result<String> uploadImage(MultipartFile file)  {
        String originalFilename= file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));

        String fileName = UUID.randomUUID().toString() + extension;

        String imageUrl = null;
        try {
            imageUrl = aliOssUtil.upload(file.getBytes(), fileName);
            return Result.success(imageUrl);
        } catch (IOException e) {
            log.error("上传失败,失败原因{}",e.getMessage());
            return Result.error("上传失败");
        }
    }
}
